package com.ccb.lms.dao;

import java.sql.SQLException;
import java.util.List;

import com.ccb.lms.model.Book;
import com.ccb.lms.util.BasePage;
import com.ibatis.sqlmap.client.SqlMapClient;

public class BookDAO implements IBookDAO {
	private SqlMapClient sqlMapClient = null;

	public SqlMapClient getSqlMapClient() {
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}

	public boolean insertBook(Book book) throws SQLException {
		
		sqlMapClient.insert("book.insertBook",book);
		return true;
	}

	public boolean deleteBook(Integer bookNO) throws SQLException {
		
		sqlMapClient.delete("book.deleteBook",bookNO);
		return true;
	}

	public boolean updateBook(Book book) throws SQLException {
		
		sqlMapClient.update("book.updateBook", book);
		return true;
	}

	@SuppressWarnings("unchecked")
	public List<Book> getAllBooks() throws SQLException {
		
		return sqlMapClient.queryForList("book.getAllBooks");
	}

	public Book getBookByNO(Integer bookNO) throws SQLException {
		return (Book) sqlMapClient.queryForObject("book.getBookByNO", bookNO);
	}

	public int getBookCount() throws SQLException {
		return (Integer) sqlMapClient.queryForObject("book.bookCount");
	}

	public BasePage getBasePage(int currentPage, int showRecordNum)
			throws SQLException {
		
		return new BasePage(currentPage, getBookCount(), showRecordNum);
	}

	@SuppressWarnings("unchecked")
	public List<Book> getBookByPage(BasePage basePage) throws SQLException {
		
		return sqlMapClient.queryForList("book.getBookByPage", basePage);
	}
	
	@SuppressWarnings("unchecked")
	public List<Book> getBookByName(String bookName) throws SQLException {
		return sqlMapClient.queryForList("book.getBookByName", bookName);
	}
	
	@SuppressWarnings("unchecked")
	public List<Book> getBookByAuthor(String authorName) throws SQLException {
		return sqlMapClient.queryForList("book.getBookByAuthor", authorName);
	}

}
